package com.nursinghome.common.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nursinghome.bean.Category;
import com.nursinghome.common.mapper.CategoryMapper;
import com.nursinghome.common.service.CategoryGoodsService;
import com.nursinghome.common.service.CategoryService;
import com.nursinghome.exception.NurisingHomeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author 程硕
* @createDate 2025-04-07 00:25:48
*/
@Service
public class CategoryServiceImpl implements CategoryService{
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private CategoryGoodsService categoryGoodsService;
    @Override
    public Boolean insert(Category category) throws NurisingHomeException {
        // 判断是否存在同名分类
        Category byName = new Category() {{
            setName(category.getName());
        }};
        Category byNameCategory = categoryMapper
                .selectAll(byName)
                .isEmpty() ? null : categoryMapper.selectAll(byName).get(0);
        if (!ObjectUtil.isEmpty(byNameCategory)){
            throw new NurisingHomeException("分类名已存在");
        }
        return categoryMapper.insert(category) > 0;
    }

    @Override
    public Boolean update(Category category) throws NurisingHomeException {
        // 判断当前分类是否存在
        Category byId = categoryMapper.selectById(category.getId());
        if (ObjectUtil.isEmpty(byId)){
            throw new NurisingHomeException("分类不存在");
        }
        if (category.getParentId() != 0){
            // 判断当前父分类下是否有同名的子分类
            Category parent = categoryMapper.selectById(category.getParentId());
            List<Category> children = category.getChildren();
            for (Category child : children) {
                if (child.getName().equals(category.getName())){
                    throw new NurisingHomeException("父分类下已有同名子分类");
                }
            }
        }
        return categoryMapper.update(category) > 0;

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(Integer id) throws NurisingHomeException {
        // 判断是否存在分类
        Category byId = categoryMapper.selectById(id);
        if (ObjectUtil.isEmpty(byId)){
            throw new NurisingHomeException("分类不存在，无法删除");
        }
        // 删除对应表的信息
        categoryGoodsService.deleteByCategoryId(id);
        byId.setIsDelete(0);
        return categoryMapper.update(byId) > 0;
    }

    @Override
    public Category selectById(Integer id) {
        return categoryMapper.selectById(id);
    }
    /*
    * 判断是否需要分页
    * 如果需要则返回分页数据
    * 不需要则返回全部数据
    * */
    @Override
    public Object selectAll(Integer pageNum, Integer pageSize, Category category) {
        if (ObjectUtil.isEmpty(pageNum) && ObjectUtil.isEmpty(pageSize)) {
            List<Category> categories = categoryMapper.selectAll(category);
            if (category != null && category.getCategoryType() == 2){
                List<Category> list = categories.stream()
                        .filter(category1 -> category1.getChildren().get(0) != null)
                        .collect(Collectors.toList());
                return list;
            }else {
                return categories;
            }

        }
        // 开启分页
        PageHelper.startPage(pageNum, pageSize);
        List<Category> categories = categoryMapper.selectAll(category);
        PageInfo<Category> pageInfo = new PageInfo<>(categories);
        return pageInfo;
    }

}
